package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsMchntHisOrderInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsMchntOrderInfo;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 商户订单表操作实现
 * @Author panky
 * @date 2020/5/18
 **/
public class CopsMchntHisOrderInfoMapperImpl implements CopsMchntHisOrderInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    @Override
    public List<MchntOrderInfo> selectMchntOrderInfoByInstIdAndMchntOrderId(String instId, String mchntNo, String mchntOrderId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectMchntHisOrderInfoByInstIdAndMchntOrderId");

        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setMchntNo(mchntNo);
        mchntOrderInfo.setMchntOrderId(mchntOrderId);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        return objects.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public MchntOrderInfo selectMchntOrderInfoByPrimaryKey(String instId, String sysSeqNum, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntHisOrderInfoByPrimaryKey");
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setSysSeqNum(sysSeqNum);
        mchntOrderInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntOrderInfo, mchntOrderInfo);
        if (0 != iReturnCode) {
            rglog.error("根据主键查询商户订单信息失败,instId:<{}>,sysSeqNum:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, sysSeqNum, mchntNo, iReturnCode);
            return null;
        }
        return mchntOrderInfo;
    }

    @Override
    public List<MchntOrderInfo> selectMchntOrderInfoByInstIdMchntNo(CopsMchntOrderInfo mchntOrderInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectMchntHisOrderInfoByInstIdMchntNo");

        if (!StringUtil.isEmptyOrNull(mchntOrderInfo.getBeginDate())) {
            sqlStr = sqlStr + " AND TXN_DATE >= " + mchntOrderInfo.getBeginDate();
        }
        if (!StringUtil.isEmptyOrNull(mchntOrderInfo.getEndDate())) {
            sqlStr = sqlStr + " AND TXN_DATE <= " + mchntOrderInfo.getEndDate();
        }
        if (!StringUtil.isEmptyOrNull(mchntOrderInfo.getMinAmt())) {
            sqlStr = sqlStr + " AND ORDER_AMT >= " + AmountUtil.changeF2Y(mchntOrderInfo.getMinAmt());
        }
        if (!StringUtil.isEmptyOrNull(mchntOrderInfo.getMaxAmt())) {
            sqlStr = sqlStr + " AND ORDER_AMT <= " + AmountUtil.changeF2Y(mchntOrderInfo.getMaxAmt());
        }

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        return objects.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntOrderInfo> selectMchntOrderInfoByInstIdSysSeqNum(String instId, String sysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectMchntHisOrderInfoSysSeqNum");

        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        mchntOrderInfo.setInstId(instId);
        mchntOrderInfo.setSysSeqNum(sysSeqNum);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, mchntOrderInfo, MchntOrderInfo.class);
        return objects.stream().map(item -> (MchntOrderInfo) item).collect(Collectors.toList());
    }
}
